package com.ark.service.impl;

import com.ark.context.BaseContext;
import com.ark.dto.ShoppingCartDTO;
import com.ark.entity.Dish;
import com.ark.entity.Setmeal;
import com.ark.entity.ShoppingCart;
import com.ark.mapper.DishMapper;
import com.ark.mapper.SetmealMapper;
import com.ark.mapper.ShoppingCartMapper;
import com.ark.service.ShoppingCartService;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.List;

/**
 * @author dyz
 * @date 2025/4/11 15:45
 */
@Service
@RequiredArgsConstructor
public class ShoppingCartServiceImpl implements ShoppingCartService {

    private final ShoppingCartMapper shoppingCartMapper;
    private final DishMapper dishMapper;
    private final SetmealMapper setmealMapper;

    @Override
    @Transactional
    public void addShoppingCart(ShoppingCartDTO shoppingCartDTO) {
        //判断当前购物车加购的数据是否已经存在
        ShoppingCart shoppingCart = new ShoppingCart();
        Long userId = BaseContext.getCurrentId();
        BeanUtils.copyProperties(shoppingCartDTO, shoppingCart);
        shoppingCart.setUserId(userId);

        List<ShoppingCart> list = shoppingCartMapper.list(shoppingCart);
        //存在则数量+1
        if(list!=null && !list.isEmpty()){
            ShoppingCart cart = list.get(0);
            cart.setNumber(cart.getNumber()+1);
            shoppingCartMapper.updateNumberById(cart);
            return;
        }
        //不存在则插入一条数据
        Long dishId = shoppingCartDTO.getDishId();
        //判断本次添加到购物车的是菜品还是套餐
        if(dishId != null){
            //本次添加进来的是菜品
            Dish dish = dishMapper.selectById(dishId);
            shoppingCart.setName(dish.getName());
            shoppingCart.setImage(dish.getImage());
            shoppingCart.setAmount(dish.getPrice());
        }else {
            //否则则为套餐
            Long setmealId = shoppingCartDTO.getSetmealId();
            Setmeal setmeal = setmealMapper.selectById(setmealId);
            shoppingCart.setName(setmeal.getName());
            shoppingCart.setImage(setmeal.getImage());
            shoppingCart.setAmount(setmeal.getPrice());
        }
        shoppingCart.setNumber(1);
        shoppingCart.setCreateTime(LocalDateTime.now());
        shoppingCartMapper.insert(shoppingCart);
    }

    @Override
    public List<ShoppingCart> showShoppingCart() {
        Long currentId = BaseContext.getCurrentId();
        ShoppingCart cart = ShoppingCart.builder()
                .userId(currentId)
                .build();
        return shoppingCartMapper.list(cart);
    }

    @Override
    public void cleanShoppingCart() {
        shoppingCartMapper.deleteByUserId(BaseContext.getCurrentId());
    }

    @Override
    @Transactional
    public void subShoppingCart(ShoppingCartDTO shoppingCartDTO) {
        ShoppingCart cart = new ShoppingCart();
        BeanUtils.copyProperties(shoppingCartDTO, cart);
        cart.setUserId(BaseContext.getCurrentId());
        //查询当前登录用户的购物车数据
        List<ShoppingCart> list = shoppingCartMapper.list(cart);
        //有的话在删除
        if(list!=null && !list.isEmpty()){
            cart = list.get(0);
            Integer number = cart.getNumber();
            if(number==1){
                //当前商品数量为1，直接删除当前记录
                shoppingCartMapper.deleteById(cart.getId());
            }else {
                //不为1,修改份数
                cart.setNumber(number-1);
                shoppingCartMapper.updateById(cart);
            }
        }
    }
}
